var host = null;

function redirect(href) {
	window.location = host+'/'+href;
}

function popup(target,callback) {
	var target_id = target.substring(0,target.indexOf('.'));
	$('#fade').attr('class','block');
	$.ajax({
		type: "GET",  
		url: host+'/ajax/'+target,  
		beforeSend: function() {
			$('.content').append("<div id='"+target_id+"' class='popup'><div class='popup-content'><div class='loader'></div></div></div>");
		},
		success: function(data) {
			$('#'+target_id+' .popup-content').html(data);
			if($('form').find('input')) {
				$('form').find('input:first').focus();
				$('form').find('input[tabindex^=1]').focus();
			}
			if(callback) {
				callback();
			}
		},
		error: function() {
			$('#'+target_id+' .popup-content').html('<p class="center">Sorry, the Ajax call could not be completed. Please try again later.');
		}
	}); 
}

function closePopup() {
	if($('#fade').attr('class','block')) {
		$('#fade').attr('class','invisible');
	}
	if($('.popup')) {
		$('.popup').remove();
	}
}

function expandCourse(target,callback) {
	target = '#'+target;
	var id = target.substring(target.indexOf('-')+1);
	$(target).find('a.expand').attr('href','javascript: collapseCourse("course-'+id+'")').attr('class','collapse');
	$.ajax({
		type: "GET",  
		url: host+'/ajax/expandcourse.php?c='+id,
		beforeSend: function() {
			$(target).append("<div class='loader'></div>");
		},
		success: function(data) {
			$(target).find('div.loader').remove();
			$(target).append(data);
			window.location.hash = target;
			if(callback) {
				callback();
			}
		},
		error: function() {
			$(target).find('div.loader').remove();
			$(target).append('<div class="course-info"><p>Sorry, the Ajax call could not be completed. Please try again later.</p></div>');
		},
	});
}

function collapseCourse(target) {
	target = '#'+target;
	var id = target.substring(target.indexOf('-')+1);
	$(target).find('a.collapse').attr('href','javascript: expandCourse("course-'+id+'")').attr('class','expand');
	$(target).find('div.course-info').remove();
	window.location.hash = '';
}

function expandGroup(target) {
	target = '#'+target;
	var id = target.substring(target.indexOf('-')+1);
	$(target).find('a.expand').attr('href','javascript: collapseGroup("group-'+id+'")').attr('class','collapse');
	$.ajax({
		type: "GET",  
		url: host+'/ajax/expandgroup.php?g='+id,
		beforeSend: function() {
			$(target).append("<div class='loader'></div>");
		},
		success: function(data) {
			$(target).find('div.loader').remove();
			$(target).append(data);
			window.location.hash = window.location.hash.split('#').splice(1,1) + target;
		},
		error: function() {
			$(target).find('div.loader').remove();
			$(target).append('<div class="students"><p>Sorry, the Ajax call could not be completed. Please try again later.</p></div>');
		}
	});
}

function collapseGroup(target) {
	target = '#'+target;
	var id = target.substring(target.indexOf('-')+1);
	$(target).find('a.collapse').attr('href','javascript: expandGroup("group-'+id+'")').attr('class','expand');
	$(target).find('div.group-info').remove();
	window.location.hash = window.location.hash.split('#').splice(1,1);
	
}

function updateGroupFeeds(group_id) {
	var target = $('#update-'+group_id);
	var timer = null;
	$.ajax({
		type: "GET",  
		url: host+'/scheduledparser.php?g='+group_id,
		beforeSend: function() {
			$(target).html('updating.');
			var count = 0;
			timer = setInterval(function(){count++;if(count <= 2) {$(target).append('.');} else {count = 0;$(target).html('updating.');}},1000);
		},
		success: function(data) {
			clearInterval(timer);
			if(data) {
				if($(target)) {
					var x = $.parseJSON(data);
					if(x.totals == 0) {
						$(target).html('feeds are up-to-date, refresh');
					} else {
						$(target).html('updates found... refreshing page.');
						window.location.hash = window.location.hash.split('#').splice(1,1) + '#group-' + group_id;
						setTimeout(window.location.reload(),1000);
					}
				}
			} else {
				$(target).html('error, try again.');
			}
		},
		error: function() {
			$(target).html('error, try again.');
		}
	});	
}

$(document).ready(function() {
	host = $('#host-name').html();
	
	if($('form').find('input')) {
		$('form').find('input:first').focus();
	}
	if(window.location.hash != '') {
		var h = window.location.hash.split('#');
		if(h[1]) {
			if(h[2]) {
				expandCourse(h[1],function() {
					var link = $('#'+h[2]).find('a.expand');
					link.attr('onclick',link.attr('href'));
					link.trigger('click');
					$('#'+h[2]).find('a.collapse').removeAttr('onclick');
				});
			} else {
				expandCourse(h[1]);
			}
		}
	}
});